package cn.fintecher.pangolin.service.dataimp.task;

import cn.fintecher.pangolin.common.enums.ImportDataExcelStatus;
import cn.fintecher.pangolin.common.model.UserModel;
import cn.fintecher.pangolin.common.utils.ZWDateUtil;
import cn.fintecher.pangolin.entity.elastic.DataImportRecord;
import cn.fintecher.pangolin.entity.elastic.OtherRecord;
import cn.fintecher.pangolin.entity.elastic.WorkOrderModel;
import cn.fintecher.pangolin.service.dataimp.elastic.DataImportRecordElastic;
import cn.fintecher.pangolin.service.dataimp.elastic.OtherRecordElastic;
import cn.fintecher.pangolin.service.dataimp.elastic.WorkOrderModelElastic;
import cn.fintecher.pangolin.common.utils.RepositoryUtil;
import org.apache.commons.collections4.IterableUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Auther: xiaqun
 * @Description:
 * @Date: 16:44 2019/11/5
 */

@Service("cancelWorkOrderTask")
public class CancelWorkOrderTask {
    @Autowired
    private WorkOrderModelElastic workOrderModelElastic;

    @Autowired
    private OtherRecordElastic otherRecordElastic;

    @Autowired
    private DataImportRecordElastic dataImportRecordElastic;

    /**
     * 撤销工单导入
     */
    @Async
    @Transactional
    public void cancelWorkOrder(BoolQueryBuilder builder, DataImportRecord dataImportRecord, UserModel userModel) {
        Iterable<WorkOrderModel> workOrderModels = workOrderModelElastic.search(builder);
        Iterable<OtherRecord> otherRecords = otherRecordElastic.search(builder);
        workOrderModels.forEach(e -> e.setFlag(1));
        otherRecords.forEach(e -> e.setFlag(1));
        RepositoryUtil.saveData(workOrderModelElastic, IterableUtils.toList(workOrderModels));
        RepositoryUtil.saveData(otherRecordElastic, IterableUtils.toList(otherRecords));
        dataImportRecord.setStatus(ImportDataExcelStatus.BILL_REVOKE);
        dataImportRecord.process(userModel.getRealName(), ZWDateUtil.getNowDateTime());
        dataImportRecordElastic.save(dataImportRecord);
    }
}
